package com.lancoo.answer.view.fragment.dictation.answer;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.lancoo.answer.R;
import com.lancoo.answer.model.ConstantBean;
import com.lancoo.answer.model.bean.Item;
import com.lancoo.answer.model.bean.Type;
import com.lancoo.answer.model.entity.TaskControlBean;
import com.lancoo.answer.util.EditTextInputUtil;
import com.lancoo.answer.util.ExamFontSizeAdjustUtils;
import com.lancoo.answer.util.SoftInputUtils;

import java.util.List;

/**
 * Created by pengmi on 2021/12/8 14:15
 * Description:
 */
public class PassageDictationPencilAnswerFragment extends Fragment {
    private View converView;

    private Item item;
    private EditText editText;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        converView=inflater.inflate(R.layout.ev_fragment_passage_dictation_answer_type_pencil,container,false);
        return converView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }


    public static Fragment loadFragment(int kindIndex, int queseIndex, int smallQueseIndex){

        PassageDictationPencilAnswerFragment fragment=new PassageDictationPencilAnswerFragment();
        Bundle mBundle=new Bundle();
        mBundle.putInt("kindIndex",kindIndex);
        mBundle.putInt("queseIndex",queseIndex);
        mBundle.putInt("smallQueseIndex",smallQueseIndex);
        fragment.setArguments(mBundle);
        return fragment;
    }

    public void setItem(Item item){
        this.item=item;
    }

    private void initView(View view){
//        if(item==null){
//            return;
//        }
        int kindIndex = getArguments().getInt("kindIndex");
        int queseIndex = getArguments().getInt("queseIndex");
        int smallQueseIndex = getArguments().getInt("smallQueseIndex");
        List<Type> kindBeanListk = ConstantBean.Companion.getConstantExamBean().getTypeList();
        item = kindBeanListk.get(kindIndex).getQuesList().get(queseIndex)
                .getChildList().get(smallQueseIndex).getItemList().get(0);

        editText = view.findViewById(R.id.edittext);
        adjustFontSize();
        editText.setText(item.getStuAnswer());
        EditTextInputUtil.disableCopyAndPaste(editText);
        SoftInputUtils.setEditTextInputSpace(editText);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                item.setStuAnswer(editable.toString().trim());
            }
        });

        TaskControlBean taskControlBean = ConstantBean.Companion.getTaskControlBean();
        if(taskControlBean!=null&&taskControlBean.getEnableAnwer()==0){//禁止作答
            editText.setEnabled(false);
        }

    }

    public void updateKeyboardShowState(boolean isShow){
        if(!isShow){
            return;
        }
//        Single.timer(100, TimeUnit.MILLISECONDS)
//                .observeOn(AndroidSchedulers.mainThread())
//                .as(RxLife.as(this))
//                .subscribe(aLong -> {
//                    if(editText!=null){
//                        editText.requestFocus();
//                    }
//                });

    }

    public void enableAnswer(){
        Log.e("1122","短文听写文本作答");
        if(editText!=null){
            editText.setEnabled(true);
        }
    }

    public void adjustFontSize(){
        if(editText!=null){
            ExamFontSizeAdjustUtils.adjustTextViewFontSize(editText,13);
        }
    }

}
